package com.goods.business.service.imp;

import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;


    //加载分类数据，返回分页数据
    @Override
    public PageVO<ProductCategoryTreeNodeVO> getCategoryList(Integer pageNum, Integer pageSize) {
        //从数据库查询所有数据
        List<ProductCategory> productCategoryList = productCategoryMapper.selectAll();
        //需要返回什么 就创建什么
        List<ProductCategoryTreeNodeVO> list = new ArrayList<>();
        //偷天换日，将ProductCategory类型数据 copy到ProductCategoryTreeNodeVO类型
        if (!CollectionUtils.isEmpty(productCategoryList)) {
            list = productCategoryList.stream().map(productCategory -> {
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
                BeanUtils.copyProperties(productCategory,productCategoryTreeNodeVO);
                return productCategoryTreeNodeVO;
            }).collect(Collectors.toList());
        }
        //调用CategoryTreeBuilder工具类，实现一二三级分类
        List<ProductCategoryTreeNodeVO> buildList = CategoryTreeBuilder.build(list);
        //调用ListPageUtils工具类实现分页
        List<ProductCategoryTreeNodeVO> page;
            if (pageNum != null && pageSize != null){
               page = ListPageUtils.page(buildList, pageSize, pageNum);
                PageVO pageVO = new PageVO(buildList.size(),page);
                return pageVO;
            }else {
                return new PageVO<>(buildList.size(),buildList);
            }
        }

    //加载父级分类数据
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryList() {

        List<ProductCategory> productCategoryList = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(productCategoryList)){
            list = productCategoryList.stream().map(productCategory -> {
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
                BeanUtils.copyProperties(productCategory,productCategoryTreeNodeVO);
                return productCategoryTreeNodeVO;
            }).collect(Collectors.toList());
        }
        //调用CategoryTreeBuilder工具类，实现一二三级分类
        List<ProductCategoryTreeNodeVO> buildList = CategoryTreeBuilder.build(list);

        return buildList;
    }

    //物资类别-添加分类
    @Override
    public void add(ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategoryMapper.insert(productCategory);
    }

    // //物质类别：打开编辑分类界面: 1.先查询 2. 回显数据到前端
    @Override
    public ProductCategoryVO edit(Long id) {
        ProductCategory productCategory = query(id);
        //2. 回显数据到前端
        ProductCategoryVO productCategoryVO = new ProductCategoryVO();
        BeanUtils.copyProperties(productCategory,productCategoryVO);
        return productCategoryVO;
    }

    //物质类别：编辑分类界面修改数据
    @Override
    public void update(Long id, ProductCategoryVO productCategoryVO) {
        productCategoryMapper.deleteByPrimaryKey(id);
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategory.setId(id);
        add(productCategoryVO);
    }

    //物质类别：删除分类
    @Override
    public void delete(Long id)  {
        ProductCategory productCategory = query(id);
        if(productCategory == null){
            try {
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Long pid = productCategory.getPid();
        Example example = new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pid",id);
        ProductCategory productCategory1 = productCategoryMapper.selectOneByExample(example);
        System.out.println("id = " + id);
        if(productCategory1 == null && pid != 0 ){
            //说明没有子菜单
            productCategoryMapper.deleteByPrimaryKey(id);
        }else {
          //存在子节点不能删除
        }
    }


    //1.查询
    public ProductCategory query(Long id){
        Example example = new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);
        ProductCategory productCategory = productCategoryMapper.selectOneByExample(example);
        return productCategory;
    }

}


